package com.psycho.sys.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.psycho.sys.api.entity.Announcement;
import com.psycho.sys.api.mapper.AnnouncementMapper;
import com.psycho.sys.api.service.AnnouncementService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 公告服务实现类
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement>
        implements AnnouncementService {

    @Override
    public Page<Announcement> pageAnnouncements(Integer current, Integer size, String title, Integer type,
            Integer status) {
        Page<Announcement> page = new Page<>(current, size);
        return baseMapper.selectAnnouncementPage(page, title, type, status);
    }

    @Override
    @Transactional
    public boolean createAnnouncement(Announcement announcement) {
        announcement.setCreateTime(LocalDateTime.now());
        announcement.setUpdateTime(LocalDateTime.now());
        announcement.setStatus(1); // 默认启用
        return save(announcement);
    }

    @Override
    @Transactional
    public boolean updateAnnouncement(Announcement announcement) {
        announcement.setUpdateTime(LocalDateTime.now());
        return updateById(announcement);
    }

    @Override
    @Transactional
    public boolean deleteAnnouncement(Long id) {
        return removeById(id);
    }

    @Override
    @Transactional
    public boolean batchDeleteAnnouncements(List<Long> ids) {
        return baseMapper.batchDeleteAnnouncements(ids) > 0;
    }

    @Override
    @Transactional
    public boolean updateAnnouncementStatus(Long id, Integer status) {
        return baseMapper.updateAnnouncementStatus(id, status) > 0;
    }

    @Override
    public List<Announcement> getLatestAnnouncements(Integer limit) {
        return baseMapper.selectLatestAnnouncements(limit);
    }

    @Override
    public List<Announcement> getActiveAnnouncements(Integer type) {
        return baseMapper.selectActiveAnnouncements(type);
    }
}